var rootPath = getRootPath();
var uid;
var isSchoolAccount;
$(function() {
	//获取跳转系统
	var system = getParentUrlParam("system");
	$.ajax({
		url: rootPath + '/user/queryUserself',
        type: 'GET',
        contentType: 'application/json;charset=UTF-8',
        success: function (data) {
        	if(data.returnCode == '0000') {
        		var user = data.returnObject;
        		var phone = user.phone;
        		//手机号码后台已用*替换
        		if(phone != undefined && phone != '' && phone.length == 11) {
        			$("#phone").html(phone);
        		}
        		$("#email").html(user.email);
        		if(data.returnObject.roles != "" && data.returnObject.roles.length > 0) {
        			data.returnObject.roles.map(function(item,index) {
        				//判断是否是学校账号
        				if(item.rleId == "20181529574197910125") {
        					isSchoolAccount = true;
        					return;
        				}
        			});
        		}

        		//添加判断是否存在微信相关信息
				if (user.unionid && user.unionid.trim()) {
					$("#bindWXStatus").html("已绑定");
					$("#rWord").hide();
					$("#unbindRWord").show();
				} else {
					$("#bindWXStatus").html("未绑定");
					$("#rWord").show();
					$("#unbindRWord").hide();
				}

        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error");
        }
    });
	$.ajax({
		url: rootPath + '/user/queryMergeInfo',
        type: 'GET',
        contentType: 'application/json;charset=UTF-8',
        success: function (res) {
        	if(res.returnCode == '2001') {
 
        	}else if(res.returnCode == '0000'){
        		var data = res.returnObject;
        		 
        		if(data.isProcessed == 0){
        			/*$("#mergeStatus").html("于 "+data.cts +" 提交的合并申请 <br />正在处理合并用户“"+data.mergeUsername+"”");*/
        			$("#mergeStatus").html('合并中');
            		$("#accountMerge").remove();  	
            		$("<li>处理中</li>").appendTo($("#merge"));
        		}else if(data.isProcessed == 1){
        			/*$("#mergeStatus").html("于 "+data.cts +" 提交的合并申请<br /> 在 "+data.mts+" 合并成功");*/
        			$("#mergeStatus").html('合并成功');
        		}else if(data.isProcessed == 2){
        			var errorMsg = "";
        			if(typeof(data.remarks)!="undefined"){
        				errorMsg = data.remarks
        			} 
        			$("#mergeStatus").html(errorMsg);
        			/*$("#mergeStatus").html("于 "+data.cts +" 提交的合并申请被驳回 <br /><font color='red'>"+errorMsg+"</font>");*/
            		$("#accountMerge").remove();  	
            		$("<li>失败</li>").appendTo($("#merge"));
        		}else{
        			console.error("合并error");
        		}

        	}else{
        		console.error("error!");
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error");
        }
    });
	
	$.ajax({
		url: rootPath + '/userFront/queryCancelInfo',
        type: 'GET',
        contentType: 'application/json;charset=UTF-8',
        success: function (res) {
        	if(res.returnCode == '2001') {
        	}else if(res.returnCode == '0000'){
        		var data = res.returnObject;
        		 
        		if(data.state == 1){
        			$("#cancelStatus").html("于 "+data.cts +" 提交的注销申请 <br />正在处理注销用户“"+data.cancelUname+"”");
            		$("#accountCancel").remove();  	
            		$("<li>处理中</li>").appendTo($("#cancel"));
        		}else if(data.state == 2){
        			$("#cancelStatus").html("于 "+data.cts +" 提交的注销申请<br /> 在 "+data.mts+" 注销成功");
        		}else if(data.state == 3){
        			var errorMsg = "";
        			if(typeof(data.remarks)!="undefined"){
        				errorMsg = data.remarks
        			} 
        			$("#cancelStatus").html("于 "+data.cts +" 提交的注销申请被驳回 <br /><font color='red'>"+errorMsg+"</font>");
            		$("#accountCancel").remove();  	
            		$("<li>失败</li>").appendTo($("#cancel"));
        		}else{
        			console.error("注销error");
        		}

        	}else{
        		console.error("error!");
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error");
        }
    });

	//OA账号业务逻辑开始
	//获取OA账号绑定状态
	$.ajax({
		url: rootPath + '/userFront/queryHrmResourceBySelf',
		type: 'GET',
		contentType: 'application/json;charset=UTF-8',
		success: function (res) {
			if(res.returnCode === '0000'){
				$("#OaBindStatus").html(res.returnObject);
				$("#OaBindUid").hide();
				$("#OaUBindUid").show();
			} else{
				$("#OaBindStatus").html("未绑定");
				$("#OaBindUid").show();
				$("#OaUBindUid").hide();
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		},
		beforeSend:function(){
			$("#OaBindStatus").html('<img src="../../imgs/ajax-loader.gif" style="width:30px;height:30px">');
			$("#OaUBindUid").hide();
			$("#OaBindUid").hide();
		}
	});

	//触发OA账号绑定
	$('#OaBindUid').click(function() {
		parent.location.href='bindOa.html'
	});
	
	//触发OA账号解绑
	$('#OaUBindUid').click(function() {
		layer.confirm('确认解绑OA账号吗?', {icon: 3,title: '提示'},function(index) {
		$.ajax({
			url: rootPath + '/userFront/unBindOaAccount',
			type: 'GET',
			contentType: 'application/json;charset=UTF-8',
			success: function (res) {
				if(res.returnCode === '2001') {
				}else if(res.returnCode === '0000'){
					$("#OaBindStatus").html("未绑定");
					$("#OaUBindUid").hide();
					$("#OaBindUid").show();
				}else{
					$("#OaBindUid").hide();
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				layer.alert("error");
			}
		});
		layer.close(index);
		});
	});

	//修改手机号码
	$('#modifyPhone').click(function() {
		parent.$("#content").prop("src","modifyPhone.html");
	});  
	
	//修改邮箱
	$("#bindEmail").click(function() {
		if(isSchoolAccount) {
			layer.alert("学校账号无法更换邮箱");
		} else {			
			parent.$("#content").prop("src","bindEmail.html");
		}
	});
	
	//绑定微信
	$("#rWord").click(function() {
		if(isSchoolAccount) {
			layer.alert("学校账号无法进行绑定微信操作");
			return;
		}
		if(isPhone) {
			parent.location.href = rootPath + "/oauth/wxapp/bind";
		} else {
			parent.$("#content").prop("src","accountWechat.html");
		}
	});
	
	//解绑微信
	$("#unbindRWord").click(function() {
		layer.confirm('确认解绑?', {icon: 3,title: '提示'},function(index) {
			$.ajax({
				url: rootPath + '/oauth/unbind',
				type: 'GET',
				data: {
					"type" : "WECHAT",
				},
				contentType: 'application/json;charset=UTF-8',
				success: function(data) {
					if(data.returnCode == '0000') {
						layer.msg('解绑成功',{icon:1,time:1000},function() {
							$("#bindWXStatus").html("未绑定");
							$("#rWord").show();
							$("#unbindRWord").hide();
							if(is_weixn()){
								windows.location.href="https://account.adream.org/account/logout/cas?service=/account";
							}
						});
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error: function(jqXHR,textStatus,errorThrown) {
					layer.alert("error");
				}
			});
			layer.close(index);
		});
	});
	
	//合并
	$("#accountMerge").click(function() {
		if(isSchoolAccount) {
			layer.alert("学校账号无法进行合并操作");
		} else {			
			parent.$("#content").prop("src","accountMerge.html");
		}
	});
	
	//注销
	$("#accountCancel").click(function() {
		if(isSchoolAccount) {
			layer.alert("学校账号无法进行注销操作");
		} else {			
			parent.$("#content").prop("src","accountCancel.html");
		}
	});
	
	//修改密码
	$("#modifyPwd").click(function() {
		$.ajax({
			url : rootPath + "/loginFront/querySelfLogins",
			method : "GET",
			success : function(data) {
				if('0000' == data.returnCode 
						&& data.returnObject != null
						&& data.returnObject.length > 0) {
					parent.$("#content").prop("src","modifyPwd.html");
				} else {
					parent.$("#content").prop("src","setNewPwd.html");
				}
			}
		});
		
	});
});

//function createQR(uid) {
function createQR(uid,system) {
	var width = window.screen.width;
	var href = rootPath + "/normal/css/layouts/";
	if(width > 600) {
		href += "bindWx/_all.css";
	} else {
		href += "menu/_all.css"; 
	}
	
	var redirectUri = redirect_uri;
	if(system != null && system != '' && system.toLowerCase() == 'scfire') {
		redirectUri += encodeURIComponent("?system=scfire")
	}
	
	var obj = new WxLogin({
		id:"wx",//div的id
		appid : appid,
		scope : scope,
//		redirect_uri : redirect_uri,
		redirect_uri : redirectUri,
		state : hex_md5("bind" + uid) + "_" + uid,
		style : style,        
		href : href
	 });
}

/**
 * 检查屏幕
 * > 600  电脑
 * @returns
 */
function isPhoneScreen() {
	var width = window.screen.width;
	var isPhone = false;
	if(width < 601) {
		isPhone = true;
	}
	return isPhone;
}
function is_weixn(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        return true;
    } else {
        return false;
    }
}
